package com.neutech.maplestory.entity;

import com.neutech.maplestory.client.MapleStoryClient;
import com.neutech.maplestory.constant.Constant;
import com.neutech.maplestory.util.ImageUtil;

import java.awt.*;
import java.util.List;

/**
 * ·@author：冯。
 * ·@version：v.1.0。
 * ·@docroot：表示产生文档的根路径。
 * ·@param：方法的参数类型。
 * ·@return：方法的返回类型。
 */
public class Item extends AbstractMapleStoryObject{

    public Image img;
    public int type;
    public boolean jump;
    public int qty = 1;
    public Item(){}

    public Item(MapleStoryClient msc,int x, int y,int type){
        this.msc = msc;
        this.x = x;
        this.y = y;
        this.jump = true;
        this.type = type;
        this.live = true;
        switch (type){
            case 0: this.img = ImageUtil.getImage("HP_50");
                break;
            case 1: this.img = ImageUtil.getImage("HP_100");
                break;
            case 2: this.img = ImageUtil.getImage("HP_300");
                break;
            case 3: this.img = ImageUtil.getImage("MP_100");
                break;
            default:
                break;
        }
        this.width = 30;
        this.height = 30;
    }

    @Override
    public void draw(Graphics g) {
        if(!live){
            msc.items.remove(this);
            return;
        }
        g.drawImage(img,x,y,null);
        move();
    }

    @Override
    public void move() {
        if (jump){
            jump(msc.bases);
        }
    }

    private double v0 = Constant.INIT_ITEM_JUMP_SPEED;
    private double vt;
    private double g = 9.8;
    private double delta_height;
    private double t = 0.5;
    /**
     * 跳跃的方法
     */
    private void jump(List<Base> bases) {
        //vt = v0 + gt    同时完成竖直上抛和自由落体
        vt = v0 - g * t;
        delta_height = v0 * t;
        v0 = vt;
        y -= delta_height;
        //自由落体的结束位置
        /*if(y >= 550){
            jump = false;
            v0 = Constant.INIT_ITEM_JUMP_SPEED;
            vt = 0.0;
            y = 550;
        }*/
        for (int i = 0; i < bases.size(); i++) {
            Base base = bases.get(i);
            if (this.getRectangle().intersects(base.getRectangle()) &&
                    this.y <= base.y - this.height + 40 +10){
                if (this.y >= base.y - this.height + 10){
                    this.y = base.y - this.height +10;
                    jump = false;
                    v0 = Constant.INIT_JUMP_SPEED;
                    vt = 0.0;
                    break;
                }
            }
        }


    }

}
